//
//  AllDetailViewController.m
//  MoGuRadio
//
//  Created by dlios on 15/6/7.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

// viewControllers
#import "AllDetailViewController.h"
#import "SpecialPageViewController.h"
#import "MyRadioViewController.h"

// 自定义cell
#import "AllPageTableViewCell.h"

// 第三方
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "UIButton+AFNetworking.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "BaseModel.h"

// 自定义类
#import "DataBaseHandler.h"
#import "AllDetail.h"
#import "MushRoom.h"

//#define TABLEVIEW_REUSE @"DetailTableViewReuseIdentifier"
#define TABLEVIEW_REUSE @"MainTableViewReuseIdentifier"

@interface AllDetailViewController () <UITableViewDelegate, UITableViewDataSource, MBProgressHUDDelegate>
{
    MBProgressHUD *hud;
}

@property (nonatomic, retain) UITableView *detailTableView;
@property (nonatomic, retain) NSMutableArray *detailArr;

@property (nonatomic, assign) CGFloat lastContentOffset;

@end

@implementation AllDetailViewController


- (void)dealloc
{

    [_detailArr release];
    [_labelId release];
    [_detailTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.2 blue:0.5 alpha:1.0];

    [self setUpSubViews];
    [self dataHandler];
//    [self.detailTableView  addHeaderWithTarget:self action:@selector(dataHandler)];
//    [self.detailTableView headerBeginRefreshing];
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.tag = 10001;
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [self MBProgressHUD];
    
}


- (void)setUpSubViews
{
    
    self.detailTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.detailTableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    self.detailTableView.delegate = self;
    self.detailTableView.dataSource = self;
    [self.view addSubview:self.detailTableView];
    [_detailTableView release];
    
    [self.detailTableView registerClass:[AllPageTableViewCell class] forCellReuseIdentifier:TABLEVIEW_REUSE];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.alldetail = self.detailArr[indexPath.row];
    
    SpecialPageViewController *specialVC = [[SpecialPageViewController alloc] init];
    
    specialVC.rid = self.alldetail.rid;
    
    specialVC.pic = self.alldetail.pic;
    
    specialVC.hostName = self.alldetail.name;
    
    specialVC.foucsNum = self.alldetail.followedNum;
    
    specialVC.listenNum = self.alldetail.listenNum;
    
    specialVC.descRadio = self.alldetail.audioDes;
    
    specialVC.likedNum = self.alldetail.likedNum;
    
    specialVC.upTime = self.alldetail.updateTime;
    
    specialVC.titleLabel = self.alldetail.rname;
    
    [self.navigationController pushViewController:specialVC animated:YES];
    //    [specialVC release];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return VIEW_HEIGHT / 7;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AllPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TABLEVIEW_REUSE];
    
    if (self.detailArr.count > indexPath.row) {
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        
        
        self.alldetail = [self.detailArr objectAtIndex:indexPath.row];
        NSString *imageStr = self.alldetail.pic;
        [cell.mainImageView setImageWithURL:[NSURL URLWithString:imageStr]];
        cell.playImageView.tag = indexPath.row + 1000;
        [cell.playImageView addGestureRecognizer:tap];
        [tap release];
        
        // 渲染图片
        UIImage *imageButton = [UIImage imageNamed:@"1.png"];
        imageButton = [imageButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [cell.addButton setImage:imageButton forState:UIControlStateNormal];
        [cell.addButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
     
        cell.likeLabel.text = @"关注";
        cell.titleLabel.text = self.alldetail.rname;
        cell.desLabel.text = self.alldetail.radioDesc;
        
        NSString *foucsStr = self.alldetail.followedNum;
        NSString *listenStr = self.alldetail.listenNum;
        cell.focusLabel.text = [BaseModel foucsChangeStringValue:foucsStr];
        cell.listenLabel.text = [BaseModel listenChangeStringValue:listenStr];
        
        cell.addButton.tag = indexPath.row;
        
    }
    
    return cell;
}



- (void)buttonAction:(UIButton *)button
{
    self.alldetail = [self.detailArr objectAtIndex:button.tag];
    
    DataBaseHandler *handler = [DataBaseHandler shareInstance];
    MushRoom *mushRoom = [[MushRoom alloc] init];
    mushRoom.rid = self.alldetail.rid;
    mushRoom.pic = self.alldetail.pic;
    mushRoom.desLabelText = self.alldetail.radioDesc;
    mushRoom.listenLabelText = self.alldetail.listenNum;
    mushRoom.focusLabelText = self.alldetail.followedNum;
    mushRoom.titleLabelText = self.alldetail.rname;
    mushRoom.hostNameText = self.alldetail.name;
    
    [handler insertMushRoom:mushRoom];
    NSLog(@"沙盒地址: %@",  NSHomeDirectory());
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 把上面playImageView的tag值传下来, 确定当前的cell
    UIImageView *myImageView = (UIImageView *) tap.view;
    self.alldetail = [self.detailArr objectAtIndex:myImageView.tag - 1000];
    MyRadioViewController *myRadioVC = [MyRadioViewController shareInstance];
    myRadioVC.rname = self.alldetail.rname;
    myRadioVC.playImage = self.alldetail.albumPic;
    myRadioVC.mp3Url = self.alldetail.mp3PlayUrl;
    
    [self.navigationController pushViewController:myRadioVC animated:YES];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithRed:0.88 green:0.90 blue:0.738 alpha:0.4];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.097 green:0.6701 blue:0.4134 alpha:1.0];
}

// 数据加载条
- (void)MBProgressHUD
{
    // 加载图MBProgressHUD
    UIView *view = [self.view viewWithTag:10001];
    
    // 方式1: 直接在view上show
    hud = [[MBProgressHUD showHUDAddedTo:view animated:YES] retain];
    hud.delegate = self;
    // 常用设置
    // 小矩形的背景颜色
    hud.color = [UIColor grayColor];
    //显示的文字
    hud.labelText = @"加载中...";
    // 是否有遮罩
    hud.dimBackground = YES;
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeAnnularDeterminate;
}

- (void)dataHandler
{
    
    NSString *dataStr = [NSString stringWithFormat:@"http://api.kaolafm.com/api/v3.5/resource/list?ids=%@_&operator=1&timestamp=1433658548&resolution=640*960&installid=0000SZ6c&version=3.5.1&appid=0&devicetype=1&page=200013&sign=F317B886396CA8621454351A56755FF7&sessionid=D9362D184A3D4602840C9AD3DE12C929&udid=22D16F7F86EBC08060D1A4AE78E40A34&network=1&channel=appstore", self.labelId];
    
    dataStr = [dataStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:dataStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSMutableArray *allArr = [[[[responseObject objectForKey:@"result"] objectForKey:@"dataList"] firstObject] objectForKey:@"dataList"];
        
        self.detailArr = [AllDetail modelArrWithDics:allArr];

//        [self.detailTableView headerEndRefreshing];
        UIView *view = [self.view viewWithTag:10001];
        [view removeFromSuperview];
        [self.detailTableView reloadData];
        [hud hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误信息是==%@", error);
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
